<?php

class ColumnAction extends BaseAction {

	public function _initialize(){
		parent::_initialize();

	}

    public function index(){
         $column2 = D('Column')->getcolumn2();
         $this->assign('column2',$column2);
    	 $this->show();
    }

    public function pagelist(){
        $url = U('Column/arclist',array('cid'=>0));
        header("location:".$url);

    }

    public function addColumn(){
        $map['cid']= I('get.pid');
        $select = D('Column')->getSelectcolumn($map['cid']);
        $pid = ($map['id'])?$map['id']:0;
        $model = D('Model')->select();

        $this->assign('select',$select);  // 栏目选项
        $this->assign('model',$model);    // 模型选项

        $this->assign('panel-title','新增栏目信息');
        $this->assign('doaction',U('Column/doaddColumn'));

        $this->show();
    }

    public function editColumn(){
        $map['cid'] = I('get.id');
        $info   = D('Column')->where($map)->find();
        $select = D('Column')->getSelectcolumn($info['pid']);
        $model  = D('Model')->select();
        $this->assign('select',$select);   // 栏目选项
        $this->assign('model',$model);     // 模型选项
        $this->assign('data',$info);       // 要修改的栏目信息
        $this->assign('panel-title','修改栏目信息');
        $this->assign('doaction',U('Column/doeditColumn'));
        $this->show('addColumn');
    }


    public function doeditColumn(){
        $info = I('post.');
        $info['pagecontent'] = $_POST['pagecontent'];
        $map['cid'] = $info['cid'];
        unset($info['cid']);
        $save = D('Column')->where($map)->save($info);
        if($save){
            $this->success('修改栏目成功！',U('Column/index'));
        }else{
            $this->error('修改栏目失败！',U('Column/editColumn',array('id'=>$map['cid'])));
        }

    }

    public function doaddColumn(){
       $data = I('post.');
       $data['pagecontent']=$_POST['pagecontent'];
       $data['sort'] = 99;
       $add = D('column')->add($data);
       if($add){
            $this->success('新增栏目成功！',U('Column/index'));
       }else{
            $this->error('新增栏目失败！',U('Column/index'));
       }
    }


    public function delColumn(){
        $id  = I('get.id');
        $ids = D('Column')->getDelcolumn($id);
        $map['cid'] = array('in',$ids);
        $del = D('Column')->where($map)->delete();
        if($del){
            $this->success('删除栏目成功！',U('Column/index'));
        }else{
            $this->error('删除栏目失败！',U('Column/index'));
        }
    }


    public function arclist(){
        import('ORG.Util.Page');// 导入分页类
        if(I('get.cid')){
            $cid = I('get.cid');
            $ids = D('Column')->getGroupcolumn($cid);
            $arr = explode(',', $ids);
            $map['cid'] = array('in', array_filter($arr));
        }else{
            $map = '1 = 1';
        }
        $count  = D('Arclist')->where($map)->count();  // 查询满足要求的总记录数
        $Page   = new Page($count,25);       // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show   = $Page->show();                    // 分页显示输出
        $list = D('Arclist')->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $select = D('Column')->getSelectcolumn($cid);
        $this->assign('select',$select);
        $this->assign('cid',$cid);
        $this->assign('list',$list);                    // 赋值数据集
        $this->assign('page',$show);                    // 赋值分页输出
        $this->show();                                  // 输出模板
    }


    /**
     * 视频管理页
     */
    public function video() {
			import('ORG.Util.Page');
			$count  = D('video')->count();
			$Page   = new Page($count,25);
			$show   = $Page->show();
			$videos = D('video')->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();

			$this->assign('videos', $videos);
			$this->assign('page',$show);
      $this->show();
    }

    /**
     * 添加视频
     */
    public function addVideo() {
        $this->show();
    }


		// 更新视频信息
		public function updateVideoInfo()
		{
			$map['id']  = I('get.id');
			$info  = D('video')->where($map)->find();  //基本数据参数

			// echo "<pre>";
			// var_dump($info);
			// echo "</pre>";
			// exit();

			$this->assign('info', $info);
			$this->show('addVideo');
		}

		// 显示游戏操作页
		public function game()
		{
			import('ORG.Util.Page');
			$count  = D('video')->count();
			$Page   = new Page($count,25);
			$show   = $Page->show();
			$games = D('game')->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();

			$this->assign('games', $games);
			$this->assign('page',$show);
			$this->show();
		}

		public function answer()
		{
			import('ORG.Util.Page');
			$count  = D('answer')->count();
			$Page   = new Page($count,25);
			$show   = $Page->show();
			$answers = D('answer')->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();

			$this->assign('answers', $answers);

			// echo '<pre>';
			// var_dump($answers);
			// echo '</pre>';
			// exit();


			$this->assign('page',$show);

			$this->show();
		}

		public function addAnswer()
		{
			$this->show();
		}

		// 添加游戏
		public function addGame()
		{
			$this->show();
		}

		// 保存疑难解答信息
		public function saveAnswerInfo()
		{
			$info = I('post.');
			// echo '<pre>';
			// var_dump($info);
			// echo '</pre>';
			// exit();

			$data = array(
				'title' => $info['title'],
				'content' => $info['content'],
				'sort' => $info['sort']
			 );

			 if ($info['edit']) {
				 $map = array('id' => $info['editId']);
				 $saveRes = D('answer')->where($map)->save($data);
				 $this->jumpPage($saveRes, '修改成功！', '修改失败！', U('Column/answer'));
			 } else {
				 $res = D('answer')->add($data);
				 $this->jumpPage($res, '保存成功！', '保存失败！', U('Column/answer'));
			 }
		}

		// 更新问题信息
		public function updateAnswerInfo()
		{
			$map['id']  = I('get.id');
			$info  = D('answer')->where($map)->find();  //基本数据参数

			// echo "<pre>";
			// var_dump($info);
			// echo "</pre>";
			// exit();

			$this->assign('data', $info);
			$this->show('addAnswer');
		}

		// 保存游戏信息
		public function saveGameInfo()
		{
			$info = I('post.');
			// echo '<pre>';
			// var_dump($info);
			// echo '</pre>';
			// exit();

			$data = array(
				'name' => $info['title'],
				'smpic' => $info['img1'],
				'despic' => $info['img2'],
				'downloadnum' => $info['keyword'],
				'description' => $info['description'],
				'content' => $info['content'],
				'sort' => $info['sort'],
				'star' => $info['star'],
				'erweima' => $info['erweima'],
				'feature' => $info['feature']
			 );

			 // 如果是编辑，则做保存操作
			 if ($info['edit']) {
				 $map = array('id' => $info['editId']);
				 $saveRes = D('game')->where($map)->save($data);
				 $this->jumpPage($saveRes, '修改成功！', '修改失败！', U('Column/game'));
			 } else {
			 	 $res = D('game')->add($data);
				 $this->jumpPage($res, '保存成功！', '保存失败！', U('Column/game'));
			 }

		}

		// 删除游戏信息
		public function delGameInfo()
		{
			$id = I('get.id');
			$map = array('id' => $id);
			$del = D('game')->where($map)->delete();
			if($del ){
				 	$this->success('删除数据成功！');
			}else{
					$this->error('删除数据失败！');
			}
		}

		// 删除问题信息
		public function delAnswerInfo()
		{
			$id = I('get.id');
			$map = array('id' => $id);
			$del = D('answer')->where($map)->delete();
			if($del ){
				 	$this->success('删除数据成功！');
			}else{
					$this->error('删除数据失败！');
			}
		}

		// 更新游戏信息
		public function updateGameInfo()
		{
			$map['id']  = I('get.id');
			$info  = D('game')->where($map)->find();  //基本数据参数

			// echo "<pre>";
			// var_dump($info);
			// echo "</pre>";
			// exit();

			$this->assign('data', $info);
			$this->show('addGame');
		}


    public function delmorearclist(){
        $info = I('post.data');
        $arr  = explode(',', $info);
        $arr  = array_filter($arr);
        $del  = D('Arclist')->delmorelist($arr);
        if($del){
            $callback = array('status'=>1,'msg'=>'SUCCESS ,删除成功！');
        }else{
            $callback = array('status'=>0,'msg'=>'ERROR ,删除失败！');
        }
        echo json_encode($callback);
        return ;
    }

    public function delarclist(){
        $id  = I('get.id');
        $del = D('Arclist')->delonelist($id);
        if($del ){
           $this->success('删除数据成功！');
        }else{
            $this->error('删除数据失败！');
        }
    }

		// 删除一条视频信息
		public function delVideoInfo()
		{
			$id = I('get.id');
			$map = array('id' => $id);
			$del = D('video')->where($map)->delete();
			if($del ){
				 	$this->success('删除数据成功！');
			}else{
					$this->error('删除数据失败！');
			}
		}

    public function addarclist(){
       $cid = I('get.cid');
       $mid = D('Column')->getMid($cid);
       $select = D('Column')->getSelectcolumn($cid);
	   $select2 = D('Column')->anliselect();
       $action = D('Model')->where('mid ='.$mid)->getField('act_add');
       $this->assign('cid',$cid);
       $this->assign('select',$select);
	    $this->assign('select2',$select2);
       $this->assign('ac',1);
       // echo $action;
       // exit();
       $this->show($action);
    }


    public function updatearclist(){
       $map['id']  = I('get.id');
       $info  = D('Arclist')->where($map)->find();  //基本数据参数
			 $info['istop'] = $info['price'];	// 这里当做是否置顶使用
       $mid   = D('Column')->getMid($info['cid']);
       $minfo = D('Model')->where('mid ='.$mid)->find();  // 获取模型数据
       $info2 = D($minfo['table'])->where('aid ='.$map['id'])->find(); // 获取辅助表数据
       $data  = ($info2)?array_merge($info,$info2):$info; // 合并整个数据
	   	$select2 = D('Column')->anliselect($data['tid']);
	   	$this->assign('select2',$select2);
       D('Arclist')->getInfo(I('get.id'));


       $this->assign('data',$data);
       $select = D('Column')->getSelectcolumn($info['cid']);
       $this->assign('cid',$cid);
       $this->assign('select',$select);
       $this->assign('ac',2);
       $this->show($minfo['act_update']);
    }

    public function doaddarclist(){

        $postinfo   = I('post.');

				// echo "<pre>";
				// var_dump($postinfo);
				// exit();
          //假如存在图片集 特殊操作
        if($postinfo['gallery']){
            $postinfo['gallery'] = implode(',', $postinfo['gallery']);
        }

        $content = strip_tags(htmlspecialchars_decode($postinfo['content']));
        $postinfo['description'] = mb_substr($content,0,200);
        $actioninfo = D('Arclist')->addActiondata($postinfo);
        $commondata = $actioninfo['commondata'];
        $infodata   = $actioninfo['infodata'];
        $infotable = D('Model')->gettable($commondata['mid']);

				$commondata['iscommend'] = $postinfo['iscommend']?1:0;
				$commondata['price'] = $postinfo['istop']?1:0; // 这是当做是否置顶使用

        M()->startTrans();
        $add1 = D('Arclist')->add($commondata);
        $infodata['aid'] = $add1;
        $add2 = D($infotable)->add($infodata);
        if($add1 && $add2){
            M()->commit();
            $this->success('添加数据成功！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }else{
            M()->rollback();
            $this->error('添加数据失败！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }
    }

    public function doupdatearclist(){
        $postinfo   = I('post.');
				$postinfo['iscommend'] = $postinfo['iscommend']?1:0;
				$postinfo['price'] = $postinfo['istop']?1:0;	// 这是当做是否置顶使用
        //假如存在图片集 特殊操作
        if($postinfo['gallery']){
            $postinfo['gallery'] = implode(',', $postinfo['gallery']);
        }
        $content = strip_tags(htmlspecialchars_decode($postinfo['content']));
        $postinfo['description'] = mb_substr($content,0,200);


        $actioninfo = D('Arclist')->addActiondata($postinfo);

        $commondata = $actioninfo['commondata'];

        $infodata   = $actioninfo['infodata'];
        $infotable  = D('Model')->gettable($commondata['mid']);

        $oldcommondata = D('Arclist')->where('id ='.$postinfo['aid'])->find();
        $oldinfodata   = D($infotable)->where('aid ='.$postinfo['aid'])->find();

        $updatecommon  = uniqueArray($oldcommondata,$commondata);
        $updateinfo    = uniqueArray($oldinfodata,$infodata);

        M()->startTrans();
        $save1 = true;
        $save2 = true;
        if($updatecommon){
            $save1 = D('Arclist')->where('id ='.$postinfo['aid'])->save($commondata);
        }
        if($updateinfo){
            D($infotable)->where('aid ='.$postinfo['aid'])->save($updateinfo);
        }

        if($save1 && $save2){
            M()->commit();
            $this->success('修改数据成功！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }else{
            M()->rollback();
            $this->error('修改数据失败！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }
    }

    public function doaddnews(){
        $postinfo = I('post.');
        $mid = D('Column')->getMid($postinfo['cid']);

        $postinfo['description'] = $postinfo['description']?$postinfo['description']:mb_strlen($postinfo['content'],100);
        $commondata = array('cid' =>$postinfo['cid'],'mid'=>$mid,'title'=>$postinfo['title'],'keyword'=>$postinfo['keyword'],'description'=>$postinfo['description'],'images'=>$postinfo['images'],'click'=>rand(1,999),'iscommend'=>$postinfo['iscommend'],'sendtime'=>time(),'updatetime'=>time());
        $infodata   = array('content' => $postinfo['content']);
        M()->startTrans();
        $add1 = D('Arclist')->add($commondata);
        $infodata['aid'] = $add1;
        $add2 = D('arclist_news')->add($infodata);
        if($add1 && $add2){
            M()->commit();
            $this->success('添加数据成功！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }else{
            M()->rollback();
            $this->error('添加数据失败！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }
    }

    public function doupdatenews(){
        $postinfo = I('post.');
        $mid = D('Column')->getMid($postinfo['cid']);
        $commondata = array('cid' =>$postinfo['cid'],'mid'=>$mid,'title'=>$postinfo['title'],'keyword'=>$postinfo['keyword'],'description'=>$postinfo['description'],'images'=>$postinfo['images'],'iscommend'=>$postinfo['iscommend'],'updatetime'=>time());
        $infodata   = array('content' => $_POST['content']);
        $commondata['description'] = $commondata['description']?$commondata['description']:mb_strlen($commondata['content'],100);


        M()->startTrans();
        $add1 = D('Arclist')->where('id ='.$postinfo['aid'])->save($commondata);
        $add2 = D('arclist_news')->where('aid ='.$postinfo['aid'])->save($infodata);
        if($add1 || $add2){
            M()->commit();
            $this->success('修改数据成功！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }else{
            M()->rollback();
            $this->error('修改数据失败！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }

    }

    //新增产品
    public function doaddshop(){
        $postinfo = I('post.');
        $mid = D('Column')->getMid($postinfo['cid']);
        $commondata = array('cid' =>$postinfo['cid'],'mid'=>$mid,'title'=>$postinfo['title'],'keyword'=>$postinfo['keyword'],'description'=>$postinfo['description'],'images'=>$postinfo['images'],'click'=>rand(1,999),'old_price'=>$postinfo['old_price'],'price'=>$postinfo['price'],'iscommend'=>$postinfo['iscommend'],'sendtime'=>time(),'updatetime'=>time());
        $infodata   = array('tmurl'=>$postinfo['tmurl'],'jdurl'=>$postinfo['jdurl'],'content' => $postinfo['content'],'service'=>$postinfo['service']);
        M()->startTrans();
        $add1 = D('Arclist')->add($commondata);
        $infodata['aid'] = $add1;
        $add2 = D('arclist_shop')->add($infodata);
        if($add1 && $add2){
            M()->commit();
            $this->success('添加数据成功！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }else{
            M()->rollback();
            $this->error('添加数据失败！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }
    }

    //修改产品数据
    public function doupdateshop(){
        $postinfo = I('post.');
        $mid = D('Column')->getMid($postinfo['cid']);
        $commondata = array('cid' =>$postinfo['cid'],'mid'=>$mid,'title'=>$postinfo['title'],'keyword'=>$postinfo['keyword'],'description'=>$postinfo['description'],'images'=>$postinfo['images'],'click'=>rand(1,999),'old_price'=>$postinfo['old_price'],'price'=>$postinfo['price'],'iscommend'=>$postinfo['iscommend'],'sendtime'=>time(),'updatetime'=>time());
        $infodata   = array('tmurl'=>$postinfo['tmurl'],'jdurl'=>$postinfo['jdurl'],'content' => $_POST['content'],'service'=>$_POST['service']);
        M()->startTrans();
        $add1 = D('Arclist')->where('id ='.$postinfo['aid'])->save($commondata);
        $add2 = D('arclist_shop')->where('aid ='.$postinfo['aid'])->save($infodata);
        if($add1 || $add2){
            M()->commit();
            $this->success('修改数据成功！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }else{
            M()->rollback();
            $this->error('修改数据失败！',U('Column/arclist',array('cid'=>$commondata['cid'])));
        }
    }


    // 留言管理
    public function message(){
        import('ORG.Util.Page');// 导入分页类
        $count  = D('message')->count();  // 查询满足要求的总记录数
        $Page   = new Page($count,25);       // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show   = $Page->show();                    // 分页显示输出
        $list =  D('message')->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);                    // 赋值数据集
        $this->assign('page',$show);                    // 赋值分页输出
        $this->show();
    }

		// 保存视频信息
		public function saveVideoInfo()
		{
			$info = $_POST;
			// echo "<pre>";
			// var_dump($info);
			// echo "</pre>";
			// exit();

			$data = array(
				'path' => $info['path'],
				'name' => $info['name'],
				'image' => $info['image'],
				'sort' => $info['sort'],
				'title' => $info['videoTitle']
			 );
			 // 如果是编辑，则做保存操作
			 if ($info['edit']) {
				 $map = array('id' => $info['editId']);
				 $saveRes = D('video')->where($map)->save($data);
				 $this->jumpPage($saveRes, '修改成功！', '修改失败！', U('Column/video'));
			 } else {
			 	 $res = D('video')->add($data);
				 $this->jumpPage($res, '保存成功！', '保存失败！', U('Column/video'));
			 }
		}

		// 跳转页面
		public function jumpPage($isSuccess, $sContent, $fContent, $url)
		{
			if ($isSuccess) {
				$this->success($sContent, $url);
			}
			else {
				$this->error($fContent, $url);
			}
		}

		public function recruitList()
		{
			$recruits = D('recruit')->select();
			$this->assign('recruits', $recruits);
			$this->show();
		}

		// 删除一条招聘信息
		public function delRecruitInfo()
		{
			$id = I('get.id');
			$map = array('id' => $id);
			$del = D('recruit')->where($map)->delete();
			if($del ){
				 	$this->success('删除数据成功！');
			}else{
					$this->error('删除数据失败！');
			}
		}

		// 招聘模块
		public function recruit($path=NULL)
		{
			$map = array('is_all' => 0);
			$category = D('recruit_category')->where($map)->select();
			$city = D('recruit_city')->where($map)->select();

			$this->assign('category', $category);
			$this->assign('city', $city);
			$this->show($path);
		}

		// 编辑招聘信息
		public function editRecruitInfo()
		{
			$id = $_GET['id'];
			$data = D('recruit')->where('id='.$id)->select();
			$recruitInfo = $data[0];

			$this->assign('recruitInfo', $recruitInfo);
			$this->recruit('recruit');

		}

		public function addRecruitInfo()
		{
			$this->recruit("recruit");
		}

		// 删除城市信息
		public function delCity()
		{
			$id = $_GET['id'];
			$del = D('recruit_city')->where('id='.$id)->delete();
			if($del){
					$this->success('删除城市成功！',U('Column/recruit'));
			}else{
					$this->error('删除城市失败！',U('Column/recruit'));
			}
		}

		// 添加城市
		public function addCity()
		{
			$data = $_POST;
			// echo "<pre>";
			// var_dump($_POST);
			$data['is_all'] = 0;

			$add = D('recruit_city')->add($data);
			if($add){
					 $this->success('新增城市成功！',U('Column/recruit'));
			}else{
					 $this->error('新增城市失败！',U('Column/recruit'));
			}
		}

		public function delCategory()
		{
			$id = $_GET['id'];
			$del = D('recruit_category')->where('id='.$id)->delete();
			if($del){
					$this->success('删除城市成功！',U('Column/recruit'));
			}else{
					$this->error('删除城市失败！',U('Column/recruit'));
			}
		}

		// 添加城市
		public function addCategory()
		{
			$data = $_POST;
			// echo "<pre>";
			// var_dump($_POST);
			$data['is_all'] = 0;

			$add = D('recruit_category')->add($data);
			if($add){
					 $this->success('新增城市成功！',U('Column/recruit'));
			}else{
					 $this->error('新增城市失败！',U('Column/recruit'));
			}
		}

		// 添加招聘信息
		public function addRecruit()
		{
			// echo "<pre>";
			// var_dump($_POST);
			// exit();
			$data = $_POST;
			$data['updatetime'] = time();

			if ($data['id']) {
				$res = D('recruit')->where('id='.$data['id'])->save($data);
				$this->jumpPage($res, '更新成功！', '更新失败！', U('Column/recruitList'));

			} else {
				$res = D('recruit')->add($data);
				$this->jumpPage($res, '添加成功！', '添加失败！', U('Column/recruitList'));
			}

		}


        public function advantage()
        {
            $advantages = D('advantage')->select();
            // echo "<pre>";
            // var_dump($advantages);
            // exit();
            $this->assign('advantages', $advantages);
            $this->show();
        }


        public function aboutUs() {
            $map = array('id' => 1);
            $res = D('about_us')->where($map)->select();
            $about = $res[0];
            $about['content'] = html_entity_decode($about['content']);
            // echo "<pre>";
            // var_dump($about);
            // exit();
            $this->assign('aboutUs', $about);
            $this->show();
        }


}
